package com.example.smartgreenhouse.controller;

import com.example.smartgreenhouse.entity.EnvironmentCurrent;
import com.example.smartgreenhouse.annotation.JwtToken;
import com.example.smartgreenhouse.annotation.Limit;
import com.example.smartgreenhouse.exception.BusinessException;
import com.example.smartgreenhouse.service.EnvironmentMetricsService;
import com.example.smartgreenhouse.utils.ResponseWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/gsas/api/v1/metrics")
public class EnvironmentMetricsController {

    private final EnvironmentMetricsService metricsService;
    private final EnvironmentMetricsService environmentCurrentService;

    // 过去24小时单指标接口
    @GetMapping("/{metric}/24h")
    @Limit(
            key = "environment24hMetric",           // 限流标识（通常作为Key）
            permitsPerSecond = 4.0,                 // 每秒许可数
            timeout = 500,                         // 尝试获取令牌的等待时间（毫秒）
            limitType = Limit.LimitType.IP,        // 限流策略（按IP限流）
            msg = "请求过于频繁，请稍后再试."      // 超限后的提示信息
    )
    public ResponseEntity<ResponseWrapper<?>> get24HourData(
            @PathVariable String metric
    ) {
        try {
            return ResponseEntity.ok(
                    ResponseWrapper.success(
                            metric+"的数据获取成功",
                            metricsService.get24HourMetricData(metric)
                    )
            );
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(ResponseWrapper.badRequest(e.getMessage()));
        } catch (Exception e) {
            log.error("获取24小时数据异常 | 指标类型: {}", metric, e);
            return ResponseEntity.internalServerError()
                    .body(ResponseWrapper.failure(500, "数据服务暂不可用"));
        }
    }

    // 过去7天单指标接口
    @GetMapping("/{metric}/7d")
    @Limit(
            key = "environment7dMetric",           // 限流标识（通常作为Key）
            permitsPerSecond = 4.0,                 // 每秒许可数
            timeout = 500,                         // 尝试获取令牌的等待时间（毫秒）
            limitType = Limit.LimitType.IP,        // 限流策略（按IP限流）
            msg = "请求过于频繁，请稍后再试."      // 超限后的提示信息
    )
    public ResponseEntity<ResponseWrapper<?>> get7DayData(
            @PathVariable String metric
    ) {
        try {
            return ResponseEntity.ok(
                    ResponseWrapper.success(
                            metric+"的数据获取成功",
                            metricsService.get7DayMetricData(metric)
                    )
            );
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(ResponseWrapper.badRequest(e.getMessage()));
        } catch (Exception e) {
            log.error("获取7天数据异常 | 指标类型: {}", metric, e);
            return ResponseEntity.internalServerError()
                    .body(ResponseWrapper.failure(500, "数据服务暂不可用"));
        }
    }

    @GetMapping("/current")
    @Limit(
            key = "Environment_current",                          // 限流标识（通常作为Key）
            permitsPerSecond = 4.0,                 // 每秒许可数
            timeout = 500,                         // 尝试获取令牌的等待时间（毫秒）
            limitType = Limit.LimitType.IP,        // 限流策略（按IP限流）
            msg = "请求过于频繁，请稍后再试."      // 超限后的提示信息
    )
    public ResponseEntity<ResponseWrapper<?>> getCurrentMetric(
            @RequestParam String metricType) {
        try {
            return ResponseEntity.ok(ResponseWrapper.success(
                    metricType + "的数据获取成功", environmentCurrentService.getByMetricType(metricType)
            ));
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(ResponseWrapper.badRequest(e.getMessage()));
        }
    }

}